package weixin;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import net.ufrog.common.dict.Element;
import weixin.processer.event.SubscribeEventProcesser;
import weixin.processer.event.UnsubscribeEventProcesser;
import weixin.processer.event.WelcomeEventProcesser;
import weixin.processer.message.EventMessageProcesser;
import weixin.processer.message.ReplyMessageProcesser;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 微信转发消息
 * 
 * @author ultrafrog
 * @version 1.0, 2014-11-03
 * @since 1.0
 */
@XStreamAlias("xml")
public class WXMessage implements Serializable {

	/**  */
	public String URL;
	
	/** 开发者微信号 */
	@XStreamAlias("ToUserName")
	public String toUserName;
	
	/** 发送者微信号 */
	@XStreamAlias("FromUserName")
	public String fromUserName;
	
	/** 创建时间 */
	@XStreamAlias("CreateTime")
	public Long createTime;
	
	/** 消息类型 */
	@XStreamAlias("MsgType")
	public String msgType;
	
	/** 文本内容 */
	@XStreamAlias("Content")
	public String content;
	
	/** 媒体编号 */
	@XStreamAlias("MediaId")
	public String mediaId;
	
	/** 图片链接 */
	@XStreamAlias("PicUrl")
	public String picUrl;
	
	/** 语音格式 */
	@XStreamAlias("Format")
	public String format;
	
	/** 缩略图编号 */
	@XStreamAlias("ThumbMediaId")
	public String thumbMediaId;
	
	/** 维度 */
	@XStreamAlias("Location_X")
	public Double locationX;
	
	/** 经度 */
	@XStreamAlias("Location_Y")
	public Double locationY;
	
	/** 缩放大小 */
	@XStreamAlias("Scale")
	public Integer scale;
	
	/** 位置信息 */
	@XStreamAlias("Label")
	public String label;
	
	/** 标题 */
	@XStreamAlias("Title")
	public String title;
	
	/** 描述 */
	@XStreamAlias("Description")
	public String description;
	
	/** 链接 */
	@XStreamAlias("Url")
	public String url;
	
	/** 消息编号 */
	@XStreamAlias("MsgId")
	public Long msgId;
	
	/** 事件类型 */
	@XStreamAlias("Event")
	public String event;
	
	/** 事件值 */
	@XStreamAlias("EventKey")
	public String eventKey;
	
	/** 票据 */
	@XStreamAlias("Ticket")
	public String ticket;
	
	/** 纬度 */
	@XStreamAlias("Latitude")
	public Double latitude;
	
	/** 经度 */
	@XStreamAlias("Longitude")
	public Double longitude;
	
	/** 精度 */
	@XStreamAlias("Precision")
	public Double precision;
	
	/** 文章数量 */
	@XStreamAlias("ArticleCount")
	public Integer articleCount;
	
	/** 文章列表 */
	@XStreamAlias("Articles")
	public List<Item> articles;
	
	/** 构造函数 */
	public WXMessage() {}
	
	/**
	 * 构造函数 
	 * 
	 * @param toUserName
	 * @param fromUserName
	 */
	public WXMessage(String toUserName, String fromUserName) {
		this();
		this.toUserName = toUserName;
		this.fromUserName = fromUserName;
		this.createTime = new Date().getTime() / 1000;
	}
	
	/**
	 * @param xml
	 * @return
	 */
	public static WXMessage fromInputStream(InputStream inputStream) {
		WXMessage message = new WXMessage();
		XStream xstream = new XStream(new DomDriver());
		xstream.processAnnotations(WXMessage.class);
		xstream.fromXML(inputStream, message);
		return message;
	}
	
	/**
	 * 消息类型
	 * 
	 * @author ultrafrog
	 * @version 1.0, 2014-11-03
	 * @since 1.0
	 */
	public static class Type {
		
		@Element("文本消息")
		@MsgType(processer = ReplyMessageProcesser.class)
		public static final String TEXT		= "text";
		
		@Element("图片消息")
		@MsgType
		public static final String IMAGE	= "image";
		
		@Element("语音消息")
		@MsgType
		public static final String VOICE	= "voice";
		
		@Element("视频消息")
		@MsgType
		public static final String VIDEO	= "video";
		
		@Element("位置消息")
		@MsgType
		public static final String LOCATION	= "location";
		
		@Element("链接消息")
		@MsgType
		public static final String LINK		= "link";
		
		@Element("事件消息")
		@MsgType(processer = EventMessageProcesser.class)
		public static final String EVENT	= "event";
		
		@Element("图文消息")
		public static final String NEWS		= "news";
	}
	
	/**
	 * 事件类型
	 * 
	 * @author ultrafrog
	 * @version 1.0, 2014-11-10
	 * @since 1.0
	 */
	public static class Event {
		
		@Element("关注")
		@EventType(processer = {SubscribeEventProcesser.class, WelcomeEventProcesser.class})
		public static final String SUBSCRIBE	= "subscribe";
		
		@Element("取消关注")
		@EventType(processer = UnsubscribeEventProcesser.class)
		public static final String UNSUBSCRIBE	= "unsubscribe";
		
		@Element("扫描")
		@EventType
		public static final String SCAN			= "SCAN";
		
		@Element("上报地理位置")
		@EventType
		public static final String LOCATION		= "LOCATION";
		
		@Element("自定义菜单")
		@EventType
		public static final String CLICK		= "CLICK";
		
		@Element("跳转")
		@EventType
		public static final String VIEW			= "VIEW";
	}
	
	/**
	 * @author ultrafrog
	 * @version 1.0, 2014-11-13
	 * @since 1.0
	 */
	@XStreamAlias("item")
	public static class Item {
		
		/** 标题 */
		@XStreamAlias("Title")
		public String title;
		
		/** 说明 */
		@XStreamAlias("Description")
		public String description;
		
		/** 图片地址 */
		@XStreamAlias("PicUrl")
		public String picUrl;
		
		/** 跳转地址 */
		@XStreamAlias("Url")
		public String url;
		
		/** 构造函数 */
		public Item() {}
		
		/**
		 * 构造函数 
		 * 
		 * @param title
		 * @param description
		 * @param picUrl
		 * @param url
		 */
		public Item(String title, String description, String picUrl, String url) {
			this();
			this.title = title;
			this.description = description;
			this.picUrl = picUrl;
			this.url = url;
		}
	}
}
